package com.cefa.edu.dao;

import com.cefa.edu.model.StudentStageData;
import com.cefa.edu.pojo.Grade;
import com.cefa.edu.pojo.OrganRelation;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author huangzehui
 * @date 18-8-10 下午1:28
 */
@Mapper
public interface OrganRelationMapper {

    /**
     * @aa 根据教育学习阶段id 删除对应的学习阶段、学校关系数据
     * @aa add by  chaijian 2018-08-15
     */
    @Update({"update cefa_edu_organ_relation set state=1 where phase_id=#{phaseId}"})
    void deleteOrganRelationByPhaseId(Integer phaseId);

    /**
     * @aa 根据班级id 删除对应的班级、年级、学习阶段、学校关系数据
     * @aa add by  chaijian 2018-08-15
     */
    @Update({"update cefa_edu_organ_relation set state=1 where class_id=#{classId}"})
    int deleteOrganRelationByClassId(Integer classId);

    /**
     * @aa 添加班级、年级、学段、学校关系数据
     * @aa add by chaijian 2018-08-15
     */
    @Insert({"INSERT into cefa_edu_organ_relation(school_id,phase_id,grade_id,class_id)values(#{schoolId},#{phaseId},#{gradeId},#{classId})"})
    int saveOrganRelationForClass(Map<String,Object> organRelMap);

    /**
     * @aa 根据班级id 获取对应的机构关系数据
     * @aa add by chaijian 2018-08-14
     */
    @Results(id = "classRelationMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "schoolId", column = "school_id"),
            @Result(property = "phaseId", column = "phase_id"),
            @Result(property = "gradeId", column = "grade_id"),
            @Result(property = "classId", column = "class_id"),
            @Result(property = "state", column = "state")
    })
    @Select({"select r.* from cefa_edu_organ_relation r where r.state=0 and r.class_id=#{classId} and r.school_id=#{schoolId}"})
    Map<String,Object> getOrganRelationBySchoolIdAndClassId(@Param("classId")Integer classId,@Param("schoolId") Integer schoolId);

    /**
     * @aa 根据年级id 获取对应的机构关系数据
     * @aa add by chaijian 2018-08-14
     */
    @Results(id = "gradeRelationMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "schoolId", column = "school_id"),
            @Result(property = "phaseId", column = "phase_id"),
            @Result(property = "gradeId", column = "grade_id"),
            @Result(property = "classId", column = "class_id"),
            @Result(property = "state", column = "state")
    })
    @Select({"select r.* from cefa_edu_organ_relation r where r.state=0 and r.grade_id=#{gradeId} and r.school_id=#{schoolId} and class_id =0"})
    Map<String,Object> getOrganRelationBySchoolIdAndGradeId(@Param("gradeId")Integer gradeId,@Param("schoolId") Integer schoolId);

    /**
     * @aa 添加学校、学校阶段、年级关系数据
     * @aa add by chaijian 2018-08-10
     * @return
     */
    @Insert({"INSERT into cefa_edu_organ_relation(school_id,phase_id,grade_id)values ${values}"})
    int saveOrganRelation(@Param("values")String values);

    @Select("select eor.class_id classId from cefa_edu_class c left join cefa_edu_organ_relation eor on c.id= eor.class_id where eor.grade_id=#{gradeId} and eor.state=0 group by eor.class_id")
    @Results(id = "organRelationMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "schoolId", column = "school_id"),
            @Result(property = "phaseId", column = "phase_id"),
            @Result(property = "gradeId", column = "grade_id"),
            @Result(property = "classId", column = "class_id"),
            @Result(property = "state", column = "state")
    })
    List<OrganRelation> findOrganRelationByGradeId(@Param("gradeId") Integer gradeId);
    @Insert({ "insert into cefa_edu_organ_relation(school_id,phase_id,grade_id,class_id,state) values(#{schoolId},#{phaseId},#{gradeId},#{classId},#{state})" })
    int insertOrganRelation(OrganRelation organRelation);

    @Update("update cefa_edu_organ_relation set state=1  WHERE grade_id = #{gradeId}")
    void deleteOrganRelationByGradeId(Integer gradeId);



    @Select("select orn.phase_id from cefa_edu_class c left join  cefa_edu_organ_relation orn on c.id=orn.class_id where c.id=#{classId} group by orn.phase_id  ")
    @ResultMap(value="organRelationMap")
    OrganRelation findOrganRelationByClassId(@Param("classId") Integer classId);

    /**
     * @aa 根据学习阶段id 判断该阶段下是否有班级
     * @aa add by chaijian 2018-08-17
     */
    @Select({"select count(r.id) from cefa_edu_organ_relation r where r.state=0 and r.phase_id=#{phaseId} and r.class_id !=0"})
    int countOrganRelation(Integer phaseId);

    @Select({"select r.* from cefa_edu_organ_relation r where r.grade_id=#{gradeId} and r.school_id=#{schoolId} limit 1"})
    @ResultMap(value="gradeRelationMap")
    Map<String,Object> getOrganRelationBySchoolIdAndGradeId2(@Param("gradeId")Integer gradeId,@Param("schoolId") Integer schoolId);

    @Select({"select r.* from cefa_edu_organ_relation r where r.class_id=#{classId} and r.school_id=#{schoolId} limit 1"})
    @ResultMap(value="classRelationMap")
    Map<String,Object> getOrganRelationBySchoolIdAndClassId2(@Param("classId")Integer classId,@Param("schoolId") Integer schoolId);
}
